/***
 * 用两个栈实现一个队列
 */
class Stack {
    public arr: number[];

    constructor() {
        this.arr = []
    }

    push(num:number) {
        this.arr.push(num)
    }

    pop() {
        return this.arr.pop()
    }

    peek() {
        return this.arr[this.arr.length - 1]
    }
    isEmpty() {
        return this.arr.length === 0
    }

}


class TwoStackImplementQueue<T> {

    public stackPush: Stack
    public stackPop: Stack

    constructor() {
        this.stackPush = new Stack()
        this.stackPop = new Stack()
    }

    // push 栈向pop 栈里倒数据
    private pushToPop() : void {
        if (this.stackPop.isEmpty()) {
            while (!this.stackPush.isEmpty()) {
                this.stackPop.push(this.stackPush.pop())
            }
        }
    }

    public add(pustInt: number) : void {
        this.stackPush.push(pustInt)
        this.pushToPop()
    }

    public poll() : number {
        if (this.stackPop.isEmpty() && this.stackPush.isEmpty()) {
            throw new Error("queue is empty")
        }
        this.pushToPop()
        return this.stackPop.peek()
    }

    public peek() : number {
        if (this.stackPop.isEmpty() && this.stackPush.isEmpty()) {
            throw new Error("queue is empty")
        }
        this.pushToPop()
        return this.stackPop.peek()
    }

}

function main() {
    let test = new TwoStackImplementQueue()
    test.add(1);
    test.add(2);
    test.add(3);
    test.add(4);
    console.log(test.peek())
    console.log(test.poll())
}
